package com.example.hospital.mis.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.example.hospital.common.R;
import com.example.hospital.mis.controller.form.user.LoginForm;
import com.example.hospital.service.LoginService;
import com.example.hospital.service.MisUserService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController("MisUserController")
@RequestMapping("/mis/user")
public class MisUserController {
  @Value("${result.key}")
  private String res;
  
  @Resource
  private MisUserService userService;
  @Resource
  private LoginService loginService;
  
  
  /**
   * 登录
   */
  @PostMapping("/login")
  public R login(@RequestBody @Valid LoginForm form) {
    String username = form.getUsername().trim();
    String password = form.getPassword().trim();
    HashMap <String, Object> loginRes = loginService.login(username, password);
    return R.ok().put(res, loginRes);
  }
  
  /**
   * 退出
   */
  @GetMapping("/logout")
  @SaCheckLogin
  public R logout() {
    StpUtil.logout();
    return R.ok();
  }
  
  
}
